package org.agile4j.patterns.singleton;

class Singleton01 {
    /**
     * 类级内部类，该内部累的事例与外部类的事例没有绑定关系，
     * 只有被调用时才会装载，从而实现延迟加载
     */
    private static class SingletonHolder {
        /**
         * 静态的初始化器，有JVM来保证线程安全
         */
        private static Singleton01 instance = new Singleton01();
    }

    private Singleton01() {
        System.out.println("111");
    }

    public static Singleton01 getInstance() {
        return SingletonHolder.instance;
    }

    public void print() {
        System.out.println("abc");
    }
}

public class SingletonDemo01 {
    public static void main(String[] args) {
        Singleton01.getInstance().print();
        Singleton01.getInstance().print();
        Singleton01.getInstance().print();
    }
}
